Skip to content

fix: don't deploy metadata files to CDN #2104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 19, 2023
Merged

fix: don't deploy metadata files to CDN #2104

merged 21 commits into from
May 19, 2023

Conversation

pieh
Copy link
Contributor

@pieh pieh commented May 11, 2023

Summary

Current strategy of hiding metadata files (creating redirects to 404) has a flaw - redirects are case sensitive, but CDN is not so that means we can still actually reach those files by messing with route casing.

As example /trace is supposed to be hidden - and initially it appears to be: https://netlify-plugin-nextjs-demo.netlify.app/trace, however https://netlify-plugin-nextjs-demo.netlify.app/tRace allow to view the trace file. Preview Deploy for this PR actually make it impossible to serve that asset regardless of casing as it's just not deployed to CDN: https://deploy-preview-2104--netlify-plugin-nextjs-demo.netlify.app/tRaCe/

This PR changes "hiding metadata files" implementation to actually prevent those files from being deployed by deleting them after all the functions bundling (that rely on those metadata files) is finished.

This change also required adjustment to 404 documents - we were not moving them from .next/server(less)/pages before - now they are moved the same as regular html files and 404 redirects were updated to match it. This was needed because we do delete .next/server(less) before deployment so those 404 documents were just lost and not available to be served.

Test plan

  1. Visit the Deploy Preview (insert link to specific page) ...

Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal

Standard checks:

  • Check the Deploy Preview's Demo site for your PR's functionality
  • Add docs when necessary

🧪 Once merged, make sure to update the version if needed and that it was published correctly.

Ref: https://github.com/netlify/pod-ecosystem-frameworks/issues/491

@netlify
Copy link

netlify bot commented May 11, 2023

Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!

Name Link
🔨 Latest commit a9b22fc
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/64674e8cf3e13d00079daac5
😎 Deploy Preview https://deploy-preview-2104--netlify-plugin-nextjs-nx-monorepo-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 11, 2023

Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!

Name Link
🔨 Latest commit a9b22fc
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/64674e8c3f78650008adb529
😎 Deploy Preview https://deploy-preview-2104--netlify-plugin-nextjs-static-root-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions github-actions bot added the type: bug code to address defects in shipped code label May 11, 2023
@netlify
Copy link

netlify bot commented May 11, 2023

Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!

Name Link
🔨 Latest commit a9b22fc
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/64674e8ceb81f4000915aed2
😎 Deploy Preview https://deploy-preview-2104--netlify-plugin-nextjs-next-auth-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 11, 2023

Deploy Preview for netlify-plugin-nextjs-demo ready!

Name Link
🔨 Latest commit a9b22fc
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/64674e8c1a83b1000805c31b
😎 Deploy Preview https://deploy-preview-2104--netlify-plugin-nextjs-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 11, 2023

Deploy Preview for netlify-plugin-nextjs-export-demo ready!

Name Link
🔨 Latest commit a9b22fc
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/64674e8c519d8a000897c74b
😎 Deploy Preview https://deploy-preview-2104--netlify-plugin-nextjs-export-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 11, 2023

Deploy Preview for nextjs-plugin-custom-routes-demo ready!

Name Link
🔨 Latest commit a9b22fc
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/64674e8c1ec9ca00085d12d4
😎 Deploy Preview https://deploy-preview-2104--nextjs-plugin-custom-routes-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 11, 2023

Deploy Preview for next-plugin-edge-middleware ready!

Name Link
🔨 Latest commit a9b22fc
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/64674e8cac8c8900088eca4c
😎 Deploy Preview https://deploy-preview-2104--next-plugin-edge-middleware.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 11, 2023

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit a9b22fc
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/64674e8c15394b00087446b8
😎 Deploy Preview https://deploy-preview-2104--next-plugin-canary.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 11, 2023

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit a9b22fc
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/64674e8c46cdef0008812df6
😎 Deploy Preview https://deploy-preview-2104--next-i18next-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

'/react-loadable-manifest.json',
'/BUILD_ID',
]
export const HIDDEN_PATHS = destr(process.env.NEXT_KEEP_METADATA_FILES)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is primarily for test/e2e as those try to check metadata files after deployment, demos + cypress tests will delete those files on the other hand

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's for E2E/CI only (primarily), we already have a CI environment variable. See a usage of it here, https://github.com/netlify/next-runtime/blob/82c1ea371bbe2194058264d7cfad912343334809/package.json#L19.

Suggested change
export const HIDDEN_PATHS = destr(process.env.NEXT_KEEP_METADATA_FILES)
export const HIDDEN_PATHS = destr(process.env.CI)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI env var is true on Netlify platform, so we can't rely on that, otherwise we won't actually delete those meta files :(

@pieh pieh changed the title fix: tRaCe fix: don't deploy metadata files to CDN May 17, 2023
@pieh pieh marked this pull request as ready for review May 18, 2023 07:37
@pieh pieh requested a review from a team as a code owner May 18, 2023 07:37
'/react-loadable-manifest.json',
'/BUILD_ID',
]
export const HIDDEN_PATHS = destr(process.env.NEXT_KEEP_METADATA_FILES)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's for E2E/CI only (primarily), we already have a CI environment variable. See a usage of it here, https://github.com/netlify/next-runtime/blob/82c1ea371bbe2194058264d7cfad912343334809/package.json#L19.

Suggested change
export const HIDDEN_PATHS = destr(process.env.NEXT_KEEP_METADATA_FILES)
export const HIDDEN_PATHS = destr(process.env.CI)

@@ -467,3 +478,10 @@ export const movePublicFiles = async ({
await copy(publicDir, `${publish}${basePath}/`)
}
}

export const removeMetadataFiles = async (publish: string) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not be faster to await Promise.all() or await Promise.allSettled()?

Copy link
Contributor Author

@pieh pieh May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally yes, but if we don't want to do 1 by 1 for perf reasons we should still limit concurrency as otherwise some starvation might happen and doing just Promise.all() might actually be slower than doing it 1 by 1

For files moving we do have concurency of number of cpu cores, with minimum of 2 (if on single core):

https://github.com/netlify/next-runtime/blob/f051e227ca10ddc228b1d044eb835fe66fd1d733/packages/runtime/src/helpers/files.ts#L152-L153

So I would suggest we do the same here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swapped to p-limit (like snippet above) in d709eb1

Copy link

@nickytonline nickytonline left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @pieh. The env var for preserving metadata files for E2E tests is not a blocker. I'll let you decide if you opt for the existing CI env var or not. 🚢

@pieh pieh added the automerge label May 19, 2023
@kodiakhq kodiakhq bot merged commit ba3a430 into main May 19, 2023
@kodiakhq kodiakhq bot deleted the tRaCe branch May 19, 2023 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants